//
//  DownloadableMenu.m
//  QuDownloader
//
//  Created by Thomas on 10/17/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//
#define DEBUG_MODE false
#import "SoftwareBuiltInMenu.h"
#import "SoftwareMenu.h"


@implementation SoftwareBuiltInMenu
-(id)init{
	NSLog(@"init");
	
	return [super init];
}
- (void)dealloc
{
	[super dealloc];  
}

-(id)initWithIdentifier:(NSString *)initId
{
	NSArray * builtinfraps = [[NSArray alloc] initWithObjects:@"Movies",@"Music",@"Photos",@"Podcasts",@"YT",@"TV",nil];
	NSMutableArray * builtinfrapsnames = [[NSMutableArray alloc] initWithObjects:@"Movies",@"Music",@"Photos",@"Podcasts",@"Youtube",@"TV Shows",nil];
	int i;//
	i=[builtinfraps count];
	//NSLog("%i",i);
	
	[self addLabel:@"com.tomcool420.Software.SoftwareMenu"];
	[self setListTitle: @"Builtin Plugins"];
	
	_items = [[NSMutableArray alloc] initWithObjects:nil];
	_options = [[NSMutableArray alloc] initWithObjects:nil];
	int counter;
	
	for( counter=0; counter < i ; counter++)
	{
		id item = [[BRTextMenuItemLayer alloc] init];
		
		NSString *builtinfrap = (NSString *)[builtinfraps objectAtIndex:counter];
		NSString *builtinfrapname =(NSString *)[builtinfrapsnames objectAtIndex:counter];
		[_options addObject:builtinfrap];
		NSLog(@"%@",builtinfrap);
		[item setTitle:builtinfrapname];
		if([self checkExists:builtinfrap])
		{
			[item setRightJustifiedText:@"Hide"];
		}
		else
		{
			[item setRightJustifiedText:@"Show"];
		}
		[_items addObject: item];
	}
	
	id list = [self list];
	[list setDatasource: self];
	return self;
}
-(BOOL)checkExists:(NSString *)thename	
{
	NSString *frapPath= [[NSString alloc] initWithFormat:@"/System/Library/CoreServices/Finder.app/Contents/PlugIns/%@.frappliance/",thename];
	NSFileManager *manager = [NSFileManager defaultManager];
	if ([manager fileExistsAtPath:frapPath])
	{
		return YES;
	}
	else
	{
		return NO;
	}
}
-(void)itemSelected:(long)fp8
{
	NSLog(@"===== builtin itemselected =====");
	NSString * thename = [_options objectAtIndex:fp8];
	NSLog(@"%@",thename);
	NSString * frapPath= [[NSString alloc] initWithFormat:@"/System/Library/CoreServices/Finder.app/Contents/PlugIns/%@.frappliance/",thename];
	NSString * bakPath= [[NSString alloc] initWithFormat:@"/System/Library/CoreServices/Finder.app/Contents/PlugIns (Disabled)/%@.bak/",thename];
	
	NSFileManager *manager = [NSFileManager defaultManager];
	if ([manager fileExistsAtPath:frapPath])
	{
		[NSTask launchedTaskWithLaunchPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"Hide" ofType:@"sh"] arguments:[NSArray arrayWithObject:thename]];
		NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.4];
		[NSThread sleepUntilDate:future];
		[self initWithIdentifier:@"101"];
		
	}
	else
	{
		[NSTask launchedTaskWithLaunchPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"Show" ofType:@"sh"] arguments:[NSArray arrayWithObject:thename]];
		NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 0.4];
		[NSThread sleepUntilDate:future];
		[self initWithIdentifier:@"101"];
	}
}

-(long)defaultIndex
{
	return 0;
}
-(void)willBeBuried
{
	NSLog(@"willBuried");
	[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:[[self list] datasource]];
	[super willBeBuried];
}

-(void)willBePushed
{
	NSLog(@"willBePushed");
	[super willBePushed];
}

-(void)willBePopped
{
	NSLog(@"willBePopped");
	[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:[[self list] datasource]];
	[super willBePopped];
}


//	Data source methods:

- (float)heightForRow:(long)row				{ return 0.0f; }
- (BOOL)rowSelectable:(long)row				{ return YES;}
- (long)itemCount							{ return (long)[_items count];}
- (id)itemForRow:(long)row					{ return [_items objectAtIndex:row]; }
- (long)rowForTitle:(id)title				{ return (long)[_items indexOfObject:title]; }
- (id)titleForRow:(long)row					{ return [[_items objectAtIndex:row] title]; }



@end
